#include "TcpConnection.h"
#include "Acceptor.h"
#include "EventLoop.h"

void onNewConnection(const TcpConnectionPtr &con){
    cout<<"建立连接，连接信息为："<<con->streamToString()<<endl;
}

void onMessage(const TcpConnectionPtr &con){
    string msg = con->receive();
    cout<<"收到消息："<<msg<<endl;
    con->send("hello handsome\n");
}

void onClose(const TcpConnectionPtr &con){
    cout<<"关闭连接，连接信息为："<<con->streamToString()<<endl;
}

int main(){
    Acceptor acc("192.168.2.57",12345);
    acc.ready();
    EventLoop eloop(acc);
    eloop.setNewConnectionCallback(onNewConnection);
    eloop.setMessageCallback(onMessage);
    eloop.setCloseCallback(onClose);
    eloop.loop();
    // TcpConnection conn(acc.m_accept());
    // while(1){
    //     cout<<"recv msg from client:"<<conn.receive()<<endl;
    //     //这个receive里面有一个换行符，需要换行符才能readline出来
    //     conn.send("hello baby\n");
    // }
    return 0;
}